<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
use Modules\Common\General\Security\Api\Gateway;
use Modules\Common\Models\SuperEnterpriseUser;
use Modules\Common\Models\SuperEnterpriseUserAccount;
use Modules\Common\Models\SystemError;
use Modules\Common\Util\Database;
use Modules\Common\Util\System;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that are not reported.
     *
     * @var array
     */
    protected $dontReport = [
        //
    ];

    /**
     * A list of the inputs that are never flashed for validation exceptions.
     *
     * @var array
     */
    protected $dontFlash = [
        'password',
        'password_confirmation',
    ];

    /**
     * Report or log an exception.
     *
     * @param \Exception $exception
     * @return void
     */
    public function report(Exception $exception)
    {
        if ($exception instanceof \Modules\Common\Exceptions\Base\Exception) {
            $exception->report();
        } else {
            // $user = System::user();
            // $userId = $user->id;
            // $userName = $user->name ?: $user->zh_name;
            // $userMobile = $user->mobile;
            $userId = "1";
            $userName = "测试数据";
            $userMobile = "18010862268";
            $path = app()->runningInConsole() ? 'null' : request()->path();
            $method = app()->runningInConsole() ? 'null' : request()->method();
            $params = app()->runningInConsole() ? '' : json_encode(request()->all());
            $clientIp = app()->runningInConsole() ? '127.0.0.1' : request()->getClientIp();
            $exceptionName = (new \ReflectionClass($exception))->getName();
          
            Log::error(
                sprintf(
                    '%s %s %s %s %s %s %s %s %s',
                    59999,
                    $method,
                    $path,
                    $clientIp,
                    $userId,
                    $userName,
                    $userMobile,
                    $exceptionName . ':' . $exception->getMessage(),
                    $params
                ),
                ['trace' => explode(PHP_EOL, $exception->getTraceAsString())]
            );
        }
        SystemError::saveNew($exception);

    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Exception $exception
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $exception)
    {
                dd($exception);
        if (config('app.debug') || $exception instanceof \Modules\Common\Exceptions\Base\Exception) {
            return parent::render($request, $exception);
        } else {
            $exceptionName = (new \ReflectionClass($exception))->getName();
            return response(json_encode(['code' => 59999, 'msg' => $exceptionName . ':' . $exception->getMessage(), 'data' => []], JSON_UNESCAPED_UNICODE));
        }
    }
}
